home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cc04.arc / YREST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-03-15  |  9.8 KB  |  530 lines

  1. /*
  2.     Alloy File Restoral Program
  3.     V4.42    02/07/84    by Kyle Powell and Mark Hurst
  4.     V4.51    04/18/84
  5.  
  6.     For use with NetWare V4.n
  7.  
  8.     Copyright (C) 1983, 1984 Novell, Inc.
  9. */
  10.  
  11. #include    "ctype.h"
  12. #include    "stdio.h"
  13.  
  14.  
  15. #define LOCALBIT    0x80
  16. #define PERMNET     0x01
  17. #define TEMPNET     0x02
  18. #define NETBITS     0x03
  19.  
  20. #define READBIT     0x01
  21. #define WRITEBIT    0x02
  22. #define OPENBIT     0x04
  23. #define CREATEBIT    0x08
  24. #define DELETEBIT    0x10
  25. #define OWNEDBIT    0x20
  26. #define SEARCHBIT    0x40
  27. #define MODIFYBIT    0x80
  28.  
  29. /* NOTE: the Archive bit was 0x80 on ShareNet V3.12.  Has now changed: */
  30. #define BACKUPBIT    0x20
  31. #define LASTBIT     0x10
  32.  
  33. extern    unsigned DOS11, DOS20;
  34. unsigned unitsize;
  35. unsigned fileswritten;
  36. unsigned restorerights;
  37.  
  38. #define     VARPATH     '^' - 'A'
  39.  
  40. #define     TRUE        1
  41. #define     FALSE        0
  42.  
  43. #define     MAXVOLUMES    16
  44. #define     MAXSPECS    100
  45.  
  46. #define     ALLMODE     1
  47. #define     MODMODE     2
  48. #define     SELMODE     3
  49.  
  50.  
  51. FILE    *log;
  52. int    source, target; /* low level I/O file numbers */
  53.  
  54. extern    char    actarget[20];
  55. extern    char    inline[80];
  56.  
  57.  
  58. int    nprinter, logcopies;
  59.  
  60.  
  61. char    *calloc(), *malloc();
  62.  
  63.  
  64. typedef struct dosfcb {
  65.     char    drive;
  66.     char    fname[8];
  67.     char    fext[3];
  68.     unsigned curblk;
  69.     unsigned recsize;
  70.     long    size;
  71.     unsigned date;
  72.     char    reserved[10];
  73.     char    relrec;
  74.     char    record[4];
  75. } FCB;
  76.  
  77. typedef struct exdirfcb {
  78.     char    dummy;
  79.     char    exset;
  80.     char    res[5];
  81.     char    flags;
  82.     char    drive;
  83.     char    fname[8];
  84.     char    fext[3];
  85.     char    gap[17];
  86.     char    size[4];
  87. } ExDirFCB;
  88.  
  89. typedef struct exdosfcb {
  90.     char    dummy;
  91.     char    exset;
  92.     char    res[5];
  93.     char    flags;
  94.     char    drive;
  95.     char    fname[8];
  96.     char    fext[3];
  97.     unsigned curblk;
  98.     unsigned recsize;
  99.     long    size;
  100.     unsigned date;
  101.     char    reserved[10];
  102.     char    relrec;
  103.     char    record[4];
  104. } ExFCB;
  105.  
  106. extern    char    pathstring[256];
  107. extern    char    privmask;
  108.  
  109. int    _fmode = 0x8000;
  110.  
  111.  
  112.  
  113. ExFCB    sourcefile;
  114. ExFCB    destfile;
  115.  
  116.  
  117.  
  118. extern    int    curtrack,dstat,istat;
  119.  
  120. extern    int    BrkFlag;
  121.  
  122. struct    fhead {
  123.     char    fflag;
  124.     char    fname[11];
  125.     int    boffset;
  126.     int    blklen;
  127.     char    data[1];
  128. };
  129.  
  130. struct fcbstruct {
  131.     char        fcbdrive;
  132.     char        fcbname[11];
  133.     int        fblock;
  134.     int        recsize;
  135.     unsigned    fsizelow;
  136.     unsigned    fsizehigh;
  137.     char        reserved[12];
  138.     char        currec;
  139.     char        rndreclow[2];
  140.     char        rndrechigh[2];
  141. };
  142.  
  143.  
  144. char    *dirhead = " DIRECTORY      ";
  145. char    twork[8208] ={0};
  146. char    mname[13] = {0};
  147.  
  148.  
  149.  
  150. abort()
  151. {
  152.     cprintf("\r\n\7Restore session terminated.\r\n");
  153. /*    if (printlog) {
  154.         prnline();
  155.         printstr("RESTORE SESSION CANCELLED\r\n");
  156.         prnline();
  157.     } */
  158.     myexit(0);
  159. }
  160.  
  161. myexit(exitcode)
  162. int    exitcode;
  163. {
  164. /*    if (printlog) {
  165.         printstr("\r\n\r\n");
  166.         prnline();
  167.         printstr("END OF RESTORE LOG REPORT\r\n");
  168.         prnline();
  169.     }
  170.     modLST(1); /* restore LST: to local and signal spooler to print */
  171. */    rewindtape();
  172.     exit(exitcode);
  173. }
  174.  
  175. /* FATAL ERROR REPORTING ROUTINE */
  176. error(message)
  177. char    *message;
  178. {
  179.     cprintf("FATAL ERROR: %s\7\r\n", message);
  180. /*    if (printlog) {
  181.         prnline();
  182.         printstr("FATAL ERROR: "); printstr(message); printstr("\r\n");
  183.         prnline();
  184.     } */
  185. }
  186.  
  187.  
  188.  
  189. rcheckbreak()
  190. {
  191.     char    ch;
  192.  
  193.     ChkKey();
  194.     if (BrkFlag) {
  195.         cprintf("\r\n\7");
  196.         if (yes("Cancel Restore Session? (Y/N) ")) {
  197.             abort();
  198.         }
  199.     }
  200. }
  201.  
  202.  
  203.  
  204. char *msfname(name)
  205. char *name;
  206. {
  207.     char    *s1;
  208.     int    i;
  209.  
  210.     s1=mname;
  211.     for (i=0;i<8;i++) {
  212.         if (*name != ' ') *s1++ = *name++;
  213.         else name++;
  214.     }
  215.     if (*name != ' ') *s1++='.';
  216.     for (i=0;i<3;i++)
  217.         if (*name != ' ') *s1++ = *name++;
  218.     *s1 = 0;
  219.     return (mname);
  220. }
  221.  
  222.  
  223.  
  224. int readfile(buff,drive)
  225. struct fhead *buff;
  226. int drive;
  227. {
  228.     struct    fcbstruct    fcb;
  229.     char    *s1,*s2;
  230.     int    i,ccode,wlen;
  231.     int    *reclow,*rechigh;
  232.  
  233.  
  234.     reclow = (int *)  fcb.rndreclow;
  235.     rechigh = (int *) fcb.rndrechigh;
  236.     s1=buff->fname;
  237.     s2=fcb.fcbname;
  238.     fcb.fcbdrive = drive;
  239.     for (i=0;i<11;i++) *s2++=*s1++;
  240.     if (buff->boffset) { /* Append To Split File */
  241.         if (openfile(&fcb)) return(-1);
  242.     }
  243.     else if (makefile(&fcb)) return(-1);
  244.     else FileAtt(&fcb,(buff->fflag) & ~LASTBIT);
  245.     fcb.recsize=1;
  246.     SetDTA(buff->data);
  247. rloop:
  248.     *reclow = buff->boffset << 13;
  249.     *rechigh = buff->boffset >> 3;
  250.     if (buff->fflag & LASTBIT) {
  251.         wlen = buff->blklen;
  252.         if (!wlen) wlen = 0x2000;
  253.         ccode =RBlkWrite(&fcb,wlen);
  254.         closefile(&fcb);
  255.         if (ccode) return (1); else return(0);
  256.  
  257.  
  258.  
  259.  
  260.  
  261.     }
  262.     if (RBlkWrite(&fcb,0x2000)) {
  263.         closefile(&fcb);
  264.  
  265.         return(1);
  266.     }
  267. reread: ccode = readtape(buff);
  268.     if (ccode==14) {
  269.         if (curtrack < 3) {
  270.             seltrack(curtrack+1);
  271.             rewindtape();
  272.             goto reread;
  273.         }
  274.         else {
  275.             cprintf("\r\n\7File continues on another tape.\r\n");
  276.             backmarks(1);
  277.             closefile(&fcb);
  278.             return(0);
  279.         }
  280.     }
  281.     else if (ccode) {
  282.         closefile(&fcb);
  283.  
  284.         return(1);
  285.     }
  286.     goto rloop;
  287. }
  288.  
  289.  
  290. skipfile(buff)
  291. char *buff;
  292. {
  293.     int    ccode;
  294.     char    *emask = "\20???????????????";
  295.  
  296. skiploop:
  297.     if (*buff & LASTBIT) return;    /*last block */
  298.     if (searchrec(buff,emask)) {
  299.         if (curtrack == 3) {
  300.             backmarks(1);
  301.             return;
  302.         }
  303.         else {
  304.             seltrack (curtrack+1);
  305.             rewindtape();
  306.             goto skiploop;
  307.         }
  308.     }
  309. }
  310. char    npath[256] = {0};
  311.  
  312. restoredir(buff,allfiles)
  313. char *buff;
  314. int allfiles;
  315. {
  316.     char    *s1,*s2,ch, *s;
  317.     int    drive,ccode,rflag,fileflag;
  318.     int    thandle;
  319.  
  320.  
  321.     cprintf("Directory: ");
  322.     cprintf(buff+80);
  323.     cprintf("\r\non ");
  324.     cprintf(buff+16);
  325.     cprintf("\r\n");
  326.  
  327.     s = buff + 80;
  328.     while (*s++) ; /* after loop *s is rights restored flag */
  329.  
  330.     if (allfiles) rflag = 1;
  331.     else {
  332.         if (!yes("Restore from this directory? (Y/N) ")) return;
  333.         rflag = 0;
  334.         while (!rflag) {
  335.             cprintf("\r\n   A: Restore to the same directory.\r\n");
  336.             cprintf("   B: Restore to a different directory.\r\n");
  337.             cprintf("   C: Restore to the default drive.\r\n\n");
  338.             cprintf ("Select option: ");
  339.             ch = getstr(inline,3);
  340.             if (ch == 3) abort();
  341.             ch = toupper(ch);
  342.             cprintf("\r\n");
  343.             if (ch == 'A') rflag = 1;
  344.             else if (ch == 'B') rflag = 2;
  345.             else if (ch == 'C') rflag = 3;
  346.         }
  347.         if (*s == 0xFF) restorerights = yes (
  348. "Do you wish to restore security information for this directory? (Y/N) ");
  349.         else restorerights = FALSE;
  350.     }
  351.     if (rflag == 1) {
  352.         drive = '^' - 'A' + 1;
  353.         if (NewTemp(drive-1,0,buff+80,&thandle)) {
  354.             if (allfiles) {
  355.                 if (MakeDir(drive-1,0,buff+80,&thandle)) {
  356.                     if (yes("\7Unable to make directory. Abort? (Y/N) "))
  357.                         abort();
  358.                     else return;
  359.                 }
  360.             }
  361.             else {
  362.                 cprintf("\7Unable to map to the directory.\r\n");
  363.                 if (yes("Do you wish to create it? (Y/N) ")) {
  364.                     if (MakeDir(drive-1,0,buff+80,&thandle)) {
  365.                         cprintf("Unable to make directory.\r\n");
  366.                         if (yes("Try a new destination? (Y/N) "))
  367.                             rflag = 2;
  368.                         else return;
  369.                     }
  370.                 }
  371.             }
  372.         }
  373.     }
  374.     if (rflag == 2) {
  375.         drive = '^' - 'A' + 1;
  376.         do {
  377.             cprintf("Enter the destination directory: ");
  378.             ch = getstr(npath,255);
  379.             if (ch==3) abort();
  380.             cprintf("\r\n");
  381.             if (ccode = NewTemp(drive-1,0,npath,&thandle)) {
  382.                 cprintf("\7Unable to map to the directory.\r\n");
  383.                 if (yes("Do you wish to create it? (Y/N) ")) {
  384.                     ccode = MakeDir(drive-1,0,npath,&thandle);
  385.                     if (ccode)
  386.                         cprintf("\7Unable to make the directory");
  387.                 }
  388.             }
  389.         } while (ccode);
  390.     }
  391.     else if (rflag == 3) drive = 0;
  392.     if (!allfiles)
  393.         allfiles = yes("Restore all files in this directory? (Y/N) ");
  394.  
  395.     if (restorerights && (*s == 0xFF)) rrestore (drive-1,s+1);
  396.  
  397.     fileflag = TRUE;
  398.     while (TRUE) {
  399.         rcheckbreak();
  400.         ccode=readtape(buff);
  401.         if (ccode==14) {    /* File Mark Detected */
  402.             ccode=skiprecs(1);
  403.             if (ccode==0) {
  404.                 backmarks(1);
  405.                 return;
  406.             }
  407.             if (curtrack ==3) {
  408.                 backmarks(2);
  409.                 return;
  410.             }
  411.             seltrack(curtrack+1);
  412.             rewindtape();
  413.         }
  414.         else {
  415.             cprintf("File ");
  416.             cprintf(msfname(buff+1));
  417.             if (allfiles) cprintf("\r\n");
  418.             else {
  419.                 if (yes(" Restore? (Y/N) ")) fileflag = TRUE;
  420.                 else fileflag = FALSE;
  421.             }
  422.             if (fileflag) {
  423.                 if (ccode = readfile(buff,drive)) {
  424.                     if (ccode == -1) {
  425.                         cprintf("Unable to open file.\r\n");
  426.                         skipfile(buff);
  427.                     }
  428.                     else {
  429.                         cprintf("Error Restoring File.\r\n");
  430.                         return;
  431.                     }
  432.                 }
  433.             }
  434.             else skipfile(buff);
  435.         }
  436.     }
  437. }
  438.  
  439.  
  440. int searchhead(buff)
  441. char    *buff;
  442. {
  443.     char    *s1,*s2;
  444.     int    i;
  445.  
  446. nexttrack:
  447.     if (skipmarks(2)) return (-1);
  448.     backmarks(1);
  449.     if (backrecs(1)) {
  450.         if (curtrack < 3) {
  451.             seltrack(curtrack+1);
  452.             rewindtape();
  453.             goto nexttrack;
  454.         }
  455.         else {
  456.             rewindtape();
  457.             return(1);
  458.         }
  459.     }
  460.     if (readtape(buff)) return (-1);
  461.     s1=buff; s2=dirhead;
  462.     for (i=0;i<16;i++) if (*s1++ != *s2++) return(-1);
  463.     skipmarks(1);
  464.     return(0);
  465. }
  466.  
  467. tapesetup(buff)
  468. char    *buff;
  469. {
  470.     char    ch;
  471.  
  472.  
  473. newtape:
  474.     rewindtape();
  475.     cprintf(
  476.     "Prepare tape in the Alloy unit and press any key to continue.\7\r\n");
  477.     ch = getch();
  478.     if (ch == 0x03) myexit();
  479.  
  480.     if (checkheader(buff)) {
  481.  
  482.         cprintf("Non Novell Tape\r\n");
  483.         goto newtape;
  484.  
  485.     }
  486. }
  487.  
  488.  
  489. main() {
  490.  
  491.     int    ccode,allfiles;
  492.  
  493.     trapbreak();
  494. nexttape:
  495.     tapesetup(twork);
  496.     allfiles = yes("Restore tape without queries? (Y/N) ");
  497.     if (allfiles) {
  498.         restorerights = yes (
  499. "Do you wish to restore security information from this tape? (Y/N) ");
  500.         if (restorerights) {
  501.             cprintf (
  502. "WARNING: To guarantee proper restoration of directory rights, trustee lists,\r\n");
  503.             cprintf (
  504. "and file flags, you should be logged in as the real SUPERVISOR user.  This is\r\n");
  505.             cprintf (
  506. "especially true if you are going to do a full system restore.  If you are not\r\n");
  507.             cprintf (
  508. "logged in as the SUPERVISOR, press ^C at the next question to exit.\r\n\r\n");
  509.         }
  510.         cprintf(
  511. "This will restore the entire tape, replacing all files without stopping.\r\n");
  512.         if (!yes("Do you wish to continue? (Y/N) ")) goto nexttape;
  513.     }
  514.     while(TRUE) {
  515.         ccode = searchhead(twork);
  516.         if (ccode==1)  {
  517.             cprintf("End of tape found.\r\n");
  518.             if (yes("Do you want to continue to a new tape? (Y/N) "))
  519.                 goto nexttape;
  520.             exit(0);
  521.         }
  522.         if (ccode) {
  523.             cprintf("Invalid tape format encountered.\r\n");
  524.             goto nexttape;
  525.         }
  526.         restoredir(twork,allfiles);
  527.     }
  528.  
  529. }
  530.